package ncit.bluetooth.settings;

import java.nio.ByteBuffer;
import java.util.HashMap;

import ncit.bluetooth.robots.Commands;

import android.content.SharedPreferences;

public class BluetoothControlSettings {

	public static final String PREFS_NAME = "SettingsFile";
	private SharedPreferences settings;
	
	public static HashMap<String, Integer> integerSettings = new HashMap<String, Integer>();
	public static HashMap<String, String> stringSettings = new HashMap<String, String>();
	
	public static int DEVICE_TYPE = 0;//the device type: 0 for NXT
									  //				 1 for Parani.
	
	
	public static String goForwardByte = "";
	public static String maxPowerForward = "";
	public static String minPowerForward = "";
	public static String goBackwardsByte = "";
	public static String maxPowerBackwards = "";
	public static String minPowerBackwards = "";
	public static String goLeftByte = "";
	public static String maxPowerLeft = "";
	public static String minPowerLeft = "";
	public static String goRightByte = "";
	public static String maxPowerRight = "";
	public static String minPowerRight = "";

	public static int leftBackMotor = 1;
	public static int rigthBackMotor  = 2;
	public static int direction = 3;

	public static int leftTouchSensor = 1;
	public static int rightTouchSensor = 2;
	public static int ultrasonic = 3;
	
	public BluetoothControlSettings(SharedPreferences settings)
	{
		this.settings = settings;
	}
	
	
	public void loadSettings()
	{	
		//-------------------------First, we get the settings from the system file
		integerSettings.put( "leftBackMotor", this.settings.getInt("leftBackMotor",  0) );// default port A
		integerSettings.put("rightBackMotor", this.settings.getInt("rightBackMotor", 1) );// default port B
		integerSettings.put("directionMotor", this.settings.getInt("directionMotor", 2) );// default port C
		integerSettings.put("leftBackTouch",  this.settings.getInt("leftBackTouch",  0) );// default port 1
		integerSettings.put("rightBackTouch",  this.settings.getInt("rightBackTouch", 1) );// default port 2
		integerSettings.put("ultrasonic",     this.settings.getInt("ultrsonic",      2) );// default port 3
		
		stringSettings.put("goForwardByte",     this.settings.getString("goForwardByte",     "A"));
		stringSettings.put("goBackwardByte",    this.settings.getString("goBackwardByte",    "B"));
		stringSettings.put("maxPowerForward",   this.settings.getString("maxPowerForward",   "0"));
		stringSettings.put("maxPowerBackwards", this.settings.getString("maxPowerBackwards", "0"));
		stringSettings.put("goLeftByte",        this.settings.getString("goRightByte",       "C"));
		stringSettings.put("goRightByte",       this.settings.getString("goLeftByte",        "D"));
		stringSettings.put("maxPowerLeft",      this.settings.getString("maxPowerLeft",      "0"));
		stringSettings.put("maxPowerRight",     this.settings.getString("maxPowerRight",     "0"));
		
		DEVICE_TYPE = this.settings.getInt("DEVICE_TYPE", 0);
	
		//--------------------------Secondly, we load the system file in the commands.
		if(BluetoothControlSettings.DEVICE_TYPE == 0)
			/**
			 * Lego Mindstorm case.
			 */
			for(String s : BluetoothControlSettings.integerSettings.keySet())
			{
				/**
				 *  We only need to retain the ports in their proper place in the class Command .
				 *  As we only have to deal with port number ranging from 0 to 3 or 0 to 2, 
				 *  we take only the last byte from the integer.
				 */

				int i = BluetoothControlSettings.integerSettings.get(s);
				byte[] b = ByteBuffer.allocate(4).putInt(i).array();

				if(s.equalsIgnoreCase("leftBackMotor")){

					Commands.LEFT_BACK_MOTOR = b[3];
					
				}else if(s.equalsIgnoreCase("rightBackMotor")){

					Commands.RIGHT_BACK_MOTOR = b[3];

				}else if(s.equalsIgnoreCase("directionMotor")){

					Commands.DIRECTION_MOTOR = b[3];

				}else if(s.equalsIgnoreCase("leftBackTouch")){
					
					Commands.LEFT_BACK_TOUCH_SENSOR = b[3];

				}else if(s.equalsIgnoreCase("rightBackTouch")){

					Commands.RIGHT_BACK_TOUCH_SENSOR = b[3];
					
				}else if(s.equalsIgnoreCase("ultrasonic")){

					Commands.ULTRASOUND_SENSOR = b[3];
					
				}
			}

		if(BluetoothControlSettings.DEVICE_TYPE == 1)
		{
			
			for(String s : BluetoothControlSettings.stringSettings.keySet())
			{
				
				if(s.equalsIgnoreCase("goForwardByte")){
					BluetoothControlSettings.goForwardByte = BluetoothControlSettings.stringSettings.get(s); 
				}else if(s.equalsIgnoreCase("goBackwardsByte")){
					BluetoothControlSettings.goBackwardsByte = BluetoothControlSettings.stringSettings.get(s);
				}else if(s.equalsIgnoreCase("goLeftByte")){
					BluetoothControlSettings.goLeftByte = BluetoothControlSettings.stringSettings.get(s);
				}else if(s.equalsIgnoreCase("goRightByte")){
					BluetoothControlSettings.goRightByte = BluetoothControlSettings.stringSettings.get(s);
				}else if(s.equalsIgnoreCase("maxPowerForward")){
					BluetoothControlSettings.maxPowerForward = BluetoothControlSettings.stringSettings.get(s);
				}else if(s.equalsIgnoreCase("maxPowerBackwards")){
					BluetoothControlSettings.maxPowerBackwards = BluetoothControlSettings.stringSettings.get(s);
				}else if(s.equalsIgnoreCase("maxPowerLeft")){
					BluetoothControlSettings.maxPowerLeft = BluetoothControlSettings.stringSettings.get(s);
				}else if(s.equalsIgnoreCase("maxPowerRight")){
					BluetoothControlSettings.maxPowerRight = BluetoothControlSettings.stringSettings.get(s);
				}
			}
			
			
		}
		
		
		
		
		
	}
	
	public void saveSettings()
	{
		SharedPreferences.Editor editor = settings.edit();

		for(String key : integerSettings.keySet())
		{
			editor.putInt(key, integerSettings.get(key));
		}
		
		for(String key: stringSettings.keySet())
		{
			editor.putString(key, stringSettings.get(key));
		}
		
		editor.putInt("DEVICE_TYPE", DEVICE_TYPE);
		
	     // Commit the edits!
	     editor.commit();
	}
	
}
